import pygame
from pygame.locals import *
from pygame import *
from guiinput import guiinput
from classes import *

class status(pygame.sprite.Sprite):
    def __init__(self, text, application):
        joyconf = config.config("joywm.xml")
        themefile = joyconf.conf.get('theme')
        themeconf = config.config(themefile)
        themename = themeconf.conf.get('name')
        # Call sprite initializer
        pygame.sprite.Sprite.__init__(self)
        # Work a litte bit on the text
        text = " " + text + " "
        # OK = False
        self.ok = False
        # Create a new spritelist
        self.messagelist = []
        # Create message-sprite
        message = loadlabel(text, application.fontsize, application.fontcolor, application.font, application.fontbold, application.fontitalic)
        # Calculate size of white background
        max_width = message.rect.width
        max_height = message.rect.height
        # Load white background and scale to max_width & max_height
        background = loadback(application.themepath + '/widgets/message_background.png', max_width, max_height, application)
        # X & Y of the message - it depends on the background
        message.rect.x = background.rect.x
        message.rect.y = background.rect.y
        # Corners
        # Corner upper left
        ul = loadimage(application.themepath + '/widgets/frame_upperleft.png', application.cubex / 2, application.cubex / 2)
        ul.rect.x = background.rect.x - application.cubex / 2
        ul.rect.y = background.rect.y - application.cubex / 2
        # Corner upper right
        ur = loadimage(application.themepath + '/widgets/frame_upperright.png', application.cubex / 2, application.cubex / 2)
        ur.rect.x = background.rect.x + background.rect.width
        ur.rect.y = background.rect.y - application.cubex / 2
        # Corner bottom left
        bl = loadimage(application.themepath + '/widgets/frame_bottomleft.png', application.cubex / 2, application.cubex / 2)
        bl.rect.x = background.rect.x - application.cubex / 2
        bl.rect.y = background.rect.y + background.rect.height
        # Corner bottom right
        br = loadimage(application.themepath + '/widgets/frame_bottomright.png', application.cubex / 2, application.cubex / 2)
        br.rect.x = background.rect.x + background.rect.width
        br.rect.y = background.rect.y + background.rect.height
        # Lines
        # Line up
        lu = loadimage(application.themepath + '/widgets/frame_upper.png', background.rect.width, application.cubex / 2)
        lu.rect.x = background.rect.x
        lu.rect.y = background.rect.y - application.cubex / 2
        # Line bottom
        lb = loadimage(application.themepath + '/widgets/frame_bottom.png', background.rect.width, application.cubex / 2)
        lb.rect.x = background.rect.x
        lb.rect.y = background.rect.y + background.rect.height
        # Line left
        ll = loadimage(application.themepath + '/widgets/frame_left.png', application.cubex / 2, background.rect.height)
        ll.rect.x = background.rect.x - application.cubex / 2
        ll.rect.y = background.rect.y
        # Line right
        lr = loadimage(application.themepath + '/widgets/frame_right.png', application.cubex / 2, background.rect.height)
        lr.rect.x = background.rect.x + background.rect.width
        lr.rect.y = background.rect.y
        # Add everything to the spritelist
        self.messagelist.append(background)
        self.messagelist.append(message)
        # Frame
        # Corner upper left
        self.messagelist.append(ul)
        # Corner upper left
        self.messagelist.append(ur)
        # Corner upper left
        self.messagelist.append(bl)
        # Corner upper left
        self.messagelist.append(br)
        # Upper line
        self.messagelist.append(lu)
        # Bottom line
        self.messagelist.append(lb)
        # Left line
        self.messagelist.append(ll)
        # Right line
        self.messagelist.append(lr)
        # Render all items in messagelist
        messagegrouprender = pygame.sprite.OrderedUpdates(self.messagelist)
        # Draw rendered group
        messagegrouprender.draw(pygame.display.get_surface())
        # Show
        pygame.display.flip()

    def delete(self, application):
        # Empty messagelist
        self.messagelist = None
        # Draw our old rendered group - now empty
        application.spritegrouprender.draw(pygame.display.get_surface())
        # Show
        pygame.display.flip()
